/*
 * @Description: 登录验证 Store
 * @Author: xuyang
 * @Date: 2019-06-09 15:06:14 
 * @Last Modified by: XuYang
 * @Last Modified time: 2019-06-15 00:04:10
 */

import { observable, action, computed, runInAction } from 'mobx';
import {
  changeMobileApi,
  changePasswordApi,
  getUserInfoApi,
  getNoticeListApi,
  getNoticeDetailApi
} from '../api/my';
export default class MyStore {
  @observable userInfo = {
    invot_sum: 0,//总邀请数
    deposit_sum: 0,//总孵化数
    assets_sum: 0.00
  }
  @observable noticeList = [];
  @observable page = 1;
  @observable pageSize = 5;
  @observable lastPage = 1;
  @observable total = 0;
  @observable noticeDetail = {}
  constructor(rootStore) {
    this.rootStore = rootStore;
  } 
  @action setNoticeList(data){
    if(data.current_page !== this.page && data.current_page !== 1){
      this.noticeList = [...this.noticeList,data.data]
    }else{
        this.noticeList = data.data;
    }
  }
  @action setNoticePage(data){
    this.page = data.current_page;
    this.pageSize = data.per_page,
    this.lastPage = data.last_page;
    this.total = data.total;
  }
  @action setNoticeDetail(data){
    this.noticeDetail = data;
  }
  @action setUserInfo(userInfo) {
    this.userInfo.invot_sum = userInfo.invot_sum;
    this.userInfo.deposit_sum = userInfo.deposit_sum;
    if(!isNaN(userInfo.assets_info.balance) && !isNaN(userInfo.assets_info.freeze) && !isNaN(userInfo.assets_info.lock)){
      this.userInfo.assets_sum = parseFloat(userInfo.assets_info.balance) + parseFloat(userInfo.assets_info.freeze)  + parseFloat(userInfo.assets_info.lock) ;
      this.userInfo.assets_sum = this.userInfo.assets_sum.toFixed(2);
    }else {
      this.userInfo.assets_sum = 0;
    }
  }
  @action getUserInfo = async (params) =>{
    try { 
      storage.load({ key: 'token' }).then(async (ret) => {
          if (ret) {
              const response = await getUserInfoApi(ret,params);
              if(response.status === 11001){
                //登录失效 重新登录
                this.rootStore.loginStore.loginInvalid();
                return;
              }  
              if (response.status === 10000) {
                  runInAction(() => {
                    this.setUserInfo(response.data)
                  });
              } else if(response.status === 90001){
                //数据为空
              }else {
                  this.rootStore.uiStore.setErrorMsg(response.message);
              }
          }
        }).catch(() => {
          this.rootStore.uiStore.setErrorMsg(response.message);
        });
    } catch(e) {
        this.rootStore.uiStore.setErrorMsg(response.message);
    }
  }
  @action changeMobile = async (params, cb) => {
    try { 
      storage.load({ key: 'token' }).then(async (ret) => {
          if (ret) {
              const response = await changeMobileApi(ret,params);
              if (response.status === 10000) {
                  runInAction(() => {
                    //更新用户信息
                    storage.remove({key:'user'})
                    storage.save({ key: 'user', data: params.mobile});
                    cb && cb();
                  });
              } else {
                  this.rootStore.uiStore.setErrorMsg(response.message);
              }
          }
        }).catch(() => {
          this.rootStore.uiStore.setErrorMsg(response.message);
        });
    } catch(e) {
        this.rootStore.uiStore.setErrorMsg(response.message);
    }
  }

  @action changePassword = async (params,cb) => {
    try { 
      storage.load({ key: 'token' }).then(async (ret) => {
          if (ret) {
              const response = await changePasswordApi(ret,params);
              if (response.status === 10000) {
                  runInAction(() => {
                    cb && cb();
                  });
              } else {
                  this.rootStore.uiStore.setErrorMsg(response.message);
              }
          }
        }).catch(() => {
          this.rootStore.uiStore.setErrorMsg(response.message);
        });
    } catch(e) {
        this.rootStore.uiStore.setErrorMsg(response.message);
    }
  }
  /**
   * 公告列表
   */
  @action getNoticeList = async () => {
    try { 
      storage.load({ key: 'token' }).then(async (ret) => {
          if (ret) {
              const response = await getNoticeListApi(ret);
              if (response.status === 10000) {
                  runInAction(() => {
                    //更新用户信息
                    this.setNoticeList(response.data)
                    this.setNoticePage(response.data)
                  });
              } else if(response.status === 90001){
                //数据为空
              }else {
                  this.rootStore.uiStore.setErrorMsg(response.message);
              }
          }
        }).catch(() => {
          this.rootStore.uiStore.setErrorMsg(response.message);
        });
    } catch(e) {
        this.rootStore.uiStore.setErrorMsg(response.message);
    }
  }
  /**
   * 公告详情
   */
  @action getNoticeDetail = async (params) => {
    try { 
      storage.load({ key: 'token' }).then(async (ret) => {
          if (ret) {
              const response = await getNoticeDetailApi(ret,params);
              if (response.status === 10000) {
                  runInAction(() => {
                    //更新用户信息
                    this.setNoticeDetail(response.data)
                  });
              } else {
                  this.rootStore.uiStore.setErrorMsg(response.message);
              }
          }
        }).catch(() => {
          this.rootStore.uiStore.setErrorMsg(response.message);
        });
    } catch(e) {
        this.rootStore.uiStore.setErrorMsg(response.message);
    }
  }
  
}